import React from 'react'

export default class CMTBox extends React.Component{
    constructor(props){
        super(props)

    }

    render(){
        return <div>
            <label htmlFor="">评论人：</label><br/>
            <input type="text" ref="user"/><br/>
            <label htmlFor="">评论内容：</label><br/>
            <textarea cols="30" rows="10" ref="content"></textarea><br/>
            <input type="button" value="发表评论" onClick={this.postComment}/>
        </div>
    }

    postComment = () => {
        if(this.refs.user.value && this.refs.content.value){
            var list = JSON.parse(localStorage.getItem('cmts') || '[]');

            var cmtInfo = {id:list.length,user:this.refs.user.value,content:this.refs.content.value}
            list.unshift(cmtInfo);
            localStorage.setItem('cmts',JSON.stringify(list));
    
            this.refs.user.value = this.refs.content.value = "";
    
            this.props.reload()
        }

    }
}
